#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pymysql
import datetime
import time
import os
import sys


def connMySQL_insert(sql,args):
        try:
            conn=pymysql.connect(host='121.201.68.21',user='jiang',passwd='jiangwenhui',db='trans_data',port=3307,charset='utf8')
            cur=conn.cursor()
            cur.execute(sql % args)
            #cur.execute('commit')
            #cur.execute('flush privileges')
            results = cur.fetchall()
            return results
            cur.close()
            conn.close()
        except pymysql.Error as e:
             print ("Mysql Error %d: %s" % (e.args[0], e.args[1]))

if __name__ == '__main__':

    time1 = datetime.datetime.now()

    time_ = str(time1)[:10] + ' 00:00:00'
    sql_1 = 'select sum(sub_count) from sub_realtime where time_ > "%s"'
    sql_2 = 'select sum(good_count) from sub_realtime where time_ > "%s"'
    sql_3 = 'select sum(total) from sub_realtime where time_ > "%s"'

    info = connMySQL_insert(sql_1,time_)
    sub_count = info[0][0]
    info = connMySQL_insert(sql_2,time_)
    good_count = info[0][0]
    info = connMySQL_insert(sql_3,time_)
    total = info[0][0]

    sql_insert = 'insert into `sub_realtime_addup` (sub_count,good_count,total,time_) VALUES (%d,%d,%f,"%s")'
    args = (sub_count,good_count,total,str(time1)[:19])

    connMySQL_insert(sql_insert,args)
    print("ok")